<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>gtest单元测试 | 课程介绍</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../chapter5/5.3_搭建持续集成环境.html" />
    
    
    <link rel="prev" href="../chapter5/5.1_lua语言介绍.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="5.2"
        data-chapter-title="gtest单元测试"
        data-filepath="chapter5/5.2_gtest单元测试.md"
        data-basepath=".."
        data-revision="Wed Oct 31 2018 20:17:52 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
                
                <li>
                    <a href="http://www.0voice.com" target="blank" class="custom-link">ntyTcp</a>
                </li>
            
            

            
            <li class="divider"></li>
            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Introduction
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter1/index.html">
            
                
                    <a href="../chapter1/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        课程介绍和项目介绍
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="chapter1/1.1_课程介绍.html">
            
                
                    <a href="../chapter1/1.1_课程介绍.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        课程介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="chapter1/1.2_项目介绍.html">
            
                
                    <a href="../chapter1/1.2_项目介绍.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        项目介绍
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter2/index.html">
            
                
                    <a href="../chapter2/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        搭建开发环境
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="chapter2/2.1_安装和配置linux系统.html">
            
                
                    <a href="../chapter2/2.1_安装和配置linux系统.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        安装和配置linux系统
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="chapter2/2.2_linux命令.html">
            
                
                    <a href="../chapter2/2.2_linux命令.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        linux命令
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="chapter2/2.3_安装开发工具.html">
            
                
                    <a href="../chapter2/2.3_安装开发工具.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        安装开发工具
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="chapter2/2.4_搭建开发环境.html">
            
                
                    <a href="../chapter2/2.4_搭建开发环境.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        搭建开发环境
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter3/index.html">
            
                
                    <a href="../chapter3/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        项目文档
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="chapter3/3.1_如何管理需求.html">
            
                
                    <a href="../chapter3/3.1_如何管理需求.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        如何管理需求
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="chapter3/3.1_如何架构设计.html">
            
                
                    <a href="../chapter3/3.1_如何架构设计.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        如何架构设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="chapter3/3.3_共享单车架构设计.html">
            
                
                    <a href="../chapter3/3.3_共享单车架构设计.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        共享单车的架构
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="chapter4/index.html">
            
                
                    <a href="../chapter4/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        敏捷开发
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="chapter4/4.1_敏捷概述.html">
            
                
                    <a href="../chapter4/4.1_敏捷概述.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        敏捷概述
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="chapter4/4.2_SCRUM介绍.html">
            
                
                    <a href="../chapter4/4.2_SCRUM介绍.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        SCRUM介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="chapter4/4.3_我们应该认识到.html">
            
                
                    <a href="../chapter4/4.3_我们应该认识到.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        我们应该认识到
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="chapter5/index.html">
            
                
                    <a href="../chapter5/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        持续集成
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="chapter5/5.1_lua语言介绍.html">
            
                
                    <a href="../chapter5/5.1_lua语言介绍.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        lua语言介绍
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="5.2" data-path="chapter5/5.2_gtest单元测试.html">
            
                
                    <a href="../chapter5/5.2_gtest单元测试.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        gtest单元测试
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="chapter5/5.3_搭建持续集成环境.html">
            
                
                    <a href="../chapter5/5.3_搭建持续集成环境.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        搭建持续集成环境
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="chapter6/index.html">
            
                
                    <a href="../chapter6/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        产品版本管理之git
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7" data-path="chapter7/index.html">
            
                
                    <a href="../chapter7/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        shell脚本
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="chapter7/7.1_什么是shell.html">
            
                
                    <a href="../chapter7/7.1_什么是shell.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        什么是shell
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="chapter7/7.2_Shell编程.html">
            
                
                    <a href="../chapter7/7.2_Shell编程.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        Shell编程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="chapter7/7.3_启动脚本实例.html">
            
                
                    <a href="../chapter7/7.3_启动脚本实例.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        启动脚本实例
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="chapter8/index.html">
            
                
                    <a href="../chapter8/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        项目编译
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="chapter8/8.1_编译原理.html">
            
                
                    <a href="../chapter8/8.1_编译原理.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        编译原理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="chapter8/8.2_makefile.html">
            
                
                    <a href="../chapter8/8.2_makefile.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                        makefile
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="chapter8/8.3_cmake.html">
            
                
                    <a href="../chapter8/8.3_cmake.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                        cmake
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="chapter8/8.4_GNU_make.html">
            
                
                    <a href="../chapter8/8.4_GNU_make.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.4.</b>
                        
                        GNU make
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="9" data-path="chapter9/index.html">
            
                
                    <a href="../chapter9/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        调试技巧
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="9.1" data-path="chapter9/9.1_gdb调试.html">
            
                
                    <a href="../chapter9/9.1_gdb调试.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.1.</b>
                        
                        gdb调试
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.2" data-path="chapter9/9.2_coredump.html">
            
                
                    <a href="../chapter9/9.2_coredump.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.2.</b>
                        
                        coredump
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.3" data-path="chapter9/9.3_内存泄漏检查.html">
            
                
                    <a href="../chapter9/9.3_内存泄漏检查.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.3.</b>
                        
                        内存泄漏检查
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10" data-path="chapter10/index.html">
            
                
                    <a href="../chapter10/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                        项目配置文件
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="10.1" data-path="chapter10/10.1_ini配置.html">
            
                
                    <a href="../chapter10/10.1_ini配置.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.1.</b>
                        
                        ini配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.2" data-path="chapter10/10.2_xml配置.html">
            
                
                    <a href="../chapter10/10.2_xml配置.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.2.</b>
                        
                        xml配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.3" data-path="chapter10/10.3_lua配置.html">
            
                
                    <a href="../chapter10/10.3_lua配置.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.3.</b>
                        
                        lua配置
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="11" data-path="chapter11/index.html">
            
                
                    <a href="../chapter11/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>11.</b>
                        
                        项目日志
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="11.1" data-path="chapter11/11.1_重定向.html">
            
                
                    <a href="../chapter11/11.1_重定向.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>11.1.</b>
                        
                        重定向
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="11.2" data-path="chapter11/11.2_log4cpp.html">
            
                
                    <a href="../chapter11/11.2_log4cpp.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>11.2.</b>
                        
                        log4cpp
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="11.3" data-path="chapter11/11.3_大型项目的日志架构.html">
            
                
                    <a href="../chapter11/11.3_大型项目的日志架构.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>11.3.</b>
                        
                        分布式系统的集中式日志解决方案
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="12" data-path="chapter12/index.html">
            
            <span><b>12.</b> 进程</span>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="12.1" data-path="chapter12/12.1_进程的概念.html">
            
                
                    <a href="../chapter12/12.1_进程的概念.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.1.</b>
                        
                        进程的概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="12.2" data-path="chapter12/12.2_如何产生一个进程.html">
            
                
                    <a href="../chapter12/12.2_如何产生一个进程.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.2.</b>
                        
                        如何产生一个进程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="12.3" data-path="chapter12/12.3_僵尸孤儿守护进程.html">
            
                
                    <a href="../chapter12/12.3_僵尸孤儿守护进程.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.3.</b>
                        
                        僵尸、孤儿和守护进程
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="13" data-path="chapter13/index.html">
            
            <span><b>13.</b> 进程间通信</span>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="13.1" data-path="chapter13/13.1_管道与命名管道.html">
            
            <span><b>13.1.</b> 管道与命名管道</span>
            
            
        </li>
    
        <li class="chapter " data-level="13.2" data-path="chapter13/13.2_共享内存.html">
            
            <span><b>13.2.</b> 共享内存</span>
            
            
        </li>
    
        <li class="chapter " data-level="13.3" data-path="chapter13/13.3_信号量.html">
            
            <span><b>13.3.</b> 信号量</span>
            
            
        </li>
    
        <li class="chapter " data-level="13.4" data-path="chapter13/13.4_信号.html">
            
            <span><b>13.4.</b> 信号</span>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="14" data-path="chapter14/index.html">
            
                
                    <a href="../chapter14/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>14.</b>
                        
                        网络协议
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="14.1" data-path="chapter14/14.1_俗话网络通信.html">
            
                
                    <a href="../chapter14/14.1_俗话网络通信.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>14.1.</b>
                        
                        俗话网络通信
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="14.2" data-path="chapter14/14.2_网络7层协议与4层协议.html">
            
                
                    <a href="../chapter14/14.2_网络7层协议与4层协议.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>14.2.</b>
                        
                        网络7层协议与4层协议
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >课程介绍</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="52-gtest&#x5355;&#x5143;&#x6D4B;&#x8BD5;">5.2 gtest&#x5355;&#x5143;&#x6D4B;&#x8BD5;</h1>
<p>gtest&#x662F;Google&#x7684;&#x4E00;&#x5957;&#x7528;&#x4E8E;&#x7F16;&#x5199;C++&#x6D4B;&#x8BD5;&#x7684;&#x6846;&#x67B6;&#xFF0C;&#x53EF;&#x4EE5;&#x8FD0;&#x884C;&#x5728;&#x5F88;&#x591A;&#x5E73;&#x53F0;&#x4E0A;&#xFF08;&#x5305;&#x62EC;Linux&#x3001;Mac OS X&#x3001;Windows&#x3001;Cygwin&#x7B49;&#x7B49;&#xFF09;&#x3002;&#x57FA;&#x4E8E;xUnit&#x67B6;&#x6784;&#x3002;&#x652F;&#x6301;&#x5F88;&#x591A;&#x597D;&#x7528;&#x7684;&#x7279;&#x6027;&#xFF0C;&#x5305;&#x62EC;&#x81EA;&#x52A8;&#x8BC6;&#x522B;&#x6D4B;&#x8BD5;&#x3001;&#x4E30;&#x5BCC;&#x7684;&#x65AD;&#x8A00;&#x3001;&#x65AD;&#x8A00;&#x81EA;&#x5B9A;&#x4E49;&#x3001;&#x6B7B;&#x4EA1;&#x6D4B;&#x8BD5;&#x3001;&#x975E;&#x7EC8;&#x6B62;&#x7684;&#x5931;&#x8D25;&#x3001;&#x751F;&#x6210;XML&#x62A5;&#x544A;&#x7B49;&#x7B49;&#x3002;</p>
<ul>
<li><a href="#1">1.gtest&#x7684;&#x4F18;&#x70B9;</a></li>
<li><a href="#2">2.&#x642D;&#x5EFA;&#x6D4B;&#x8BD5;&#x6846;&#x67B6;</a></li>
<li><a href="#3">3.&#x6D4B;&#x8BD5;Demo</a></li>
<li><a href="#4">4.gtest&#x7684;&#x5176;&#x5B83;&#x7279;&#x6027;</a><ul>
<li><a href="#41">4.1.&#x4E8B;&#x4EF6;</a></li>
<li><a href="#42">4.2.&#x53C2;&#x6570;&#x5316;</a></li>
<li><a href="#43">4.3.&#x6B7B;&#x4EA1;&#x6D4B;&#x8BD5;</a></li>
<li><a href="#44">4.4.&#x6DF1;&#x5165;&#x89E3;&#x6790;gtest&#x6846;&#x67B6;</a>  </li>
</ul>
</li>
<li><a href="#5">5.&#x642D;&#x5EFA;&#x6D4B;&#x8BD5;&#x5DE5;&#x7A0B;</a></li>
</ul>
<h2 id="1">1.gtest&#x7684;&#x4F18;&#x70B9;</h2>

<p>&#x597D;&#x7684;&#x6D4B;&#x8BD5;&#x5E94;&#x8BE5;&#x6709;&#x4E0B;&#x9762;&#x7684;&#x8FD9;&#x4E9B;&#x7279;&#x70B9;&#xFF0C;&#x6211;&#x4EEC;&#x770B;&#x770B;GTest&#x662F;&#x5982;&#x4F55;&#x6EE1;&#x8DB3;&#x8981;&#x6C42;&#x7684;&#x3002;</p>
<ul>
<li>&#x6D4B;&#x8BD5;&#x5E94;&#x8BE5;&#x662F;&#x72EC;&#x7ACB;&#x7684;&#x3001;&#x53EF;&#x91CD;&#x590D;&#x7684;&#x3002;&#x4E00;&#x4E2A;&#x6D4B;&#x8BD5;&#x7684;&#x7ED3;&#x679C;&#x4E0D;&#x5E94;&#x8BE5;&#x4F5C;&#x4E3A;&#x53E6;&#x4E00;&#x4E2A;&#x6D4B;&#x8BD5;&#x7684;&#x524D;&#x63D0;&#x3002;GTest&#x4E2D;&#x6BCF;&#x4E2A;&#x6D4B;&#x8BD5;&#x8FD0;&#x884C;&#x5728;&#x72EC;&#x7ACB;&#x7684;&#x5BF9;&#x8C61;&#x4E2D;&#x3002;&#x5982;&#x679C;&#x67D0;&#x4E2A;&#x6D4B;&#x8BD5;&#x5931;&#x8D25;&#x4E86;&#xFF0C;&#x53EF;&#x4EE5;&#x5355;&#x72EC;&#x5730;&#x8C03;&#x8BD5;&#x5B83;&#x3002;</li>
<li>&#x6D4B;&#x8BD5;&#x5E94;&#x8BE5;&#x662F;&#x6709;&#x6E05;&#x6670;&#x7684;&#x7ED3;&#x6784;&#x7684;&#x3002;GTest&#x7684;&#x6D4B;&#x8BD5;&#x6709;&#x5F88;&#x597D;&#x7684;&#x7EC4;&#x7EC7;&#x7ED3;&#x6784;&#xFF0C;&#x6613;&#x4E8E;&#x7EF4;&#x62A4;&#x3002;</li>
<li>&#x6D4B;&#x8BD5;&#x5E94;&#x8BE5;&#x662F;&#x53EF;&#x79FB;&#x690D;&#x548C;&#x53EF;&#x590D;&#x7528;&#x7684;&#x3002;&#x6709;&#x5F88;&#x591A;&#x4EE3;&#x7801;&#x662F;&#x4E0D;&#x4F9D;&#x8D56;&#x5E73;&#x53F0;&#x7684;&#xFF0C;&#x56E0;&#x6B64;&#x5B83;&#x4EEC;&#x7684;&#x6D4B;&#x8BD5;&#x4E5F;&#x9700;&#x8981;&#x4E0D;&#x4F9D;&#x8D56;&#x4E8E;&#x5E73;&#x53F0;&#x3002;GTest&#x53EF;&#x4EE5;&#x5728;&#x591A;&#x79CD;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x3001;&#x591A;&#x79CD;&#x7F16;&#x8BD1;&#x5668;&#x4E0B;&#x5DE5;&#x4F5C;&#xFF0C;&#x6709;&#x5F88;&#x597D;&#x7684;&#x53EF;&#x79FB;&#x690D;&#x6027;&#x3002;</li>
<li>&#x6D4B;&#x8BD5;&#x5931;&#x8D25;&#x65F6;&#xFF0C;&#x5E94;&#x8BE5;&#x7ED9;&#x51FA;&#x5C3D;&#x53EF;&#x80FD;&#x8BE6;&#x5C3D;&#x7684;&#x4FE1;&#x606F;&#x3002;GTest&#x5728;&#x9047;&#x5230;&#x5931;&#x8D25;&#x65F6;&#x5E76;&#x4E0D;&#x505C;&#x6B62;&#x63A5;&#x4E0B;&#x6765;&#x7684;&#x6D4B;&#x8BD5;&#xFF0C;&#x800C;&#x4E14;&#x8FD8;&#x53EF;&#x4EE5;&#x9009;&#x62E9;&#x4F7F;&#x7528;&#x975E;&#x7EC8;&#x6B62;&#x7684;&#x5931;&#x8D25;&#x6765;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#x5F53;&#x524D;&#x7684;&#x6D4B;&#x8BD5;&#x3002;&#x8FD9;&#x6837;&#x4E00;&#x6B21;&#x53EF;&#x4EE5;&#x6D4B;&#x8BD5;&#x5C3D;&#x53EF;&#x80FD;&#x591A;&#x7684;&#x95EE;&#x9898;&#x3002;</li>
<li>&#x6D4B;&#x8BD5;&#x6846;&#x67B6;&#x5E94;&#x8BE5;&#x907F;&#x514D;&#x8BA9;&#x5F00;&#x53D1;&#x8005;&#x7EF4;&#x62A4;&#x6D4B;&#x8BD5;&#x6846;&#x67B6;&#x76F8;&#x5173;&#x7684;&#x4E1C;&#x897F;&#x3002;GTest&#x53EF;&#x4EE5;&#x81EA;&#x52A8;&#x8BC6;&#x522B;&#x5B9A;&#x4E49;&#x7684;&#x5168;&#x90E8;&#x6D4B;&#x8BD5;&#xFF0C;&#x4F60;&#x4E0D;&#x9700;&#x8981;&#x4E00;&#x4E00;&#x5217;&#x4E3E;&#x5B83;&#x4EEC;&#x3002;</li>
<li>&#x6D4B;&#x8BD5;&#x5E94;&#x8BE5;&#x591F;&#x5FEB;&#x3002;GTest&#x5728;&#x6EE1;&#x8DB3;&#x6D4B;&#x8BD5;&#x72EC;&#x7ACB;&#x7684;&#x524D;&#x63D0;&#x4E0B;&#xFF0C;&#x5141;&#x8BB8;&#x4F60;&#x590D;&#x7528;&#x5171;&#x4EAB;&#x6570;&#x636E;&#xFF0C;&#x5B83;&#x4EEC;&#x53EA;&#x9700;&#x521B;&#x5EFA;&#x4E00;&#x6B21;&#x3002;</li>
<li>GTest&#x91C7;&#x7528;&#x7684;&#x662F;xUnit&#x67B6;&#x6784;&#xFF0C;&#x4F60;&#x4F1A;&#x53D1;&#x73B0;&#x548C;JUnit&#x3001;PyUnit&#x5F88;&#x7C7B;&#x4F3C;&#xFF0C;&#x6240;&#x4EE5;&#x4E0A;&#x624B;&#x975E;&#x5E38;&#x5FEB;&#x3002;</li>
</ul>
<h2 id="2">2.&#x642D;&#x5EFA;&#x6D4B;&#x8BD5;&#x6846;&#x67B6;</h2>  

<p>gtest&#x4E0B;&#x8F7D;&#x5730;&#x5740;&#xFF1A; <a href="https://github.com/google/googletest" target="_blank">https://github.com/google/googletest</a>  </p>
<p>&#x4E0B;&#x8F7D;&#x65B9;&#x6CD5;&#x662F;&#xFF1A;git clone <a href="https://github.com/google/googletest.git" target="_blank">https://github.com/google/googletest.git</a><br>&#x5B89;&#x88C5;&#x65B9;&#x6CD5;&#x662F;&#xFF1A;  </p>
<pre><code>$ cd googletest
$ cmake .
$ make
</code></pre><p>&#x7136;&#x540E;&#x5728;lib&#x76EE;&#x5F55;&#x4E0B;&#x4F1A;&#x751F;&#x6210;&#xFF1A;libgmock.a  libgmock_main.a  libgtest.a  libgtest_main.a<br>&#x6700;&#x540E;&#x6211;&#x4EEC;&#x518D;<code>sudo make install</code>&#x3002;</p>
<h2 id="3">3.&#x6D4B;&#x8BD5;Demo</h2>  

<ul>
<li>&#x7B2C;&#x4E00;&#x6B65;&#xFF1A;&#x5047;&#x8BBE;&#x6211;&#x4EEC;&#x7684;&#x5B9E;&#x73B0;&#x4E24;&#x4E2A;&#x51FD;&#x6570;:  </li>
</ul>
<pre><code>
#include &quot;sample1.h&quot;

// Returns n! (the factorial of n).  For negative n, n! is defined to be 1.
int Factorial(int n) {
  int result = 1;
  for (int i = 1; i &lt;= n; i++) {
    result *= i;
  }

  return result;
}

// Returns true iff n is a prime number.
bool IsPrime(int n) {
  // Trivial case 1: small numbers
  if (n &lt;= 1) return false;

  // Trivial case 2: even numbers
  if (n % 2 == 0) return n == 2;

  // Now, we have that n is odd and n &gt;= 3.

  // Try to divide n by every odd number i, starting from 3
  for (int i = 3; ; i += 2) {
    // We only have to try i up to the square root of n
    if (i &gt; n/i) break;

    // Now, we have i &lt;= n/i &lt; n.
    // If n is divisible by i, n is not prime.
    if (n % i == 0) return false;
  }

  // n has no integer factor in the range (1, n), and thus is prime.
  return true;
}
</code></pre><p>&#x8FD9;&#x4E24;&#x4E2A;&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x5728;sample1.cc&#x6587;&#x4EF6;&#x91CC;&#xFF0C;&#x51FD;&#x6570;&#x7533;&#x660E;&#x5728;sample1.h&#x91CC;&#xFF1A;</p>
<pre><code>#ifndef GTEST_SAMPLES_SAMPLE1_H_
#define GTEST_SAMPLES_SAMPLE1_H_

// Returns n! (the factorial of n).  For negative n, n! is defined to be 1.
int Factorial(int n);

// Returns true iff n is a prime number.
bool IsPrime(int n);

#endif
</code></pre><ul>
<li>&#x7B2C;&#x4E8C;&#x6B65;&#xFF1A;&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x5C31;&#x662F;&#x8981;&#x6D4B;&#x8BD5;Factorial&#x548C;IsPrime&#x4E24;&#x4E2A;&#x51FD;&#x6570;&#x662F;&#x5426;&#x6B63;&#x786E;&#xFF0C;&#x597D;&#x4E86;&#x5F00;&#x59CB;&#x5199;&#x6211;&#x4EEC;&#x7684;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x628A;&#x3002;&#x65B0;&#x5EFA;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#xFF0C;&#x547D;&#x540D;&#x4E3A;sample_unittest.cc&#xFF0C;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;  </li>
</ul>
<pre><code>#include &lt;limits.h&gt;
#include &quot;sample1.h&quot;
#include &quot;gtest/gtest.h&quot;
namespace {

TEST(FactorialTest, Negative) {
    // This test is named &quot;Negative&quot;, and belongs to the &quot;FactorialTest&quot;
    // test case.
    EXPECT_EQ(1, Factorial(-5));
    EXPECT_EQ(1, Factorial(-1));
    EXPECT_GT(Factorial(-10), 0);
}

TEST(FactorialTest, Zero) {
    EXPECT_EQ(1, Factorial(0));
}

TEST(FactorialTest, Positive) {
    EXPECT_EQ(1, Factorial(1));
    EXPECT_EQ(2, Factorial(2));
    EXPECT_EQ(6, Factorial(3));
    EXPECT_EQ(40320, Factorial(8));
}

// Tests IsPrime()
TEST(IsPrimeTest, Negative) {
  EXPECT_FALSE(IsPrime(-1));
  EXPECT_FALSE(IsPrime(-2));
  EXPECT_FALSE(IsPrime(INT_MIN));
}

TEST(IsPrimeTest, Trivial) {
  EXPECT_FALSE(IsPrime(0));
  EXPECT_FALSE(IsPrime(1));
  EXPECT_TRUE(IsPrime(2));
  EXPECT_TRUE(IsPrime(3));
}

TEST(IsPrimeTest, Positive) {
  EXPECT_FALSE(IsPrime(4));
  EXPECT_TRUE(IsPrime(5));
  EXPECT_FALSE(IsPrime(6));
  EXPECT_TRUE(IsPrime(23));
}
}  // namespace
</code></pre><p>TEST&#x662F;gtest&#x7684;&#x6D4B;&#x8BD5;&#x5B8F;&#xFF0C;&#x6211;&#x4EEC;&#x7684;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x5FC5;&#x987B;&#x6309;&#x7167;&#x8FD9;&#x6837;&#x683C;&#x5F0F;&#x5199;&#xFF0C;isPrimeTest&#x662F;&#x6D4B;&#x8BD5;&#x5957;&#x7684;&#x540D;&#x5B57;&#xFF0C;&#x4E00;&#x4E2A;&#x6D4B;&#x8BD5;&#x5957;&#x4E0B;&#x53EF;&#x4EE5;&#x6709;&#x591A;&#x4E2A;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#xFF0C;&#x90A3;&#x4E48;Positive&#x3001;Trivial&#x5C31;&#x662F;&#x6211;&#x4EEC;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x7684;&#x540D;&#x79F0;&#xFF0C;EXPECT_EQ&#x3001;EXPECT_FALSE&#x548C;EXPECT_TRUE&#x7B49;&#x7B49;&#xFF0C;&#x90FD;&#x662F;gtest&#x63D0;&#x4F9B;&#x7684;&#x6D4B;&#x8BD5;&#x65AD;&#x8A00;&#xFF0C;&#x6BD4;&#x5982;<code>EXPECT_EQ(1, Factorial(1));</code>&#x5C31;&#x662F;&#x8868;&#x793A;Factorial(1)&#x548C;1&#x662F;&#x4E0D;&#x662F;&#x76F8;&#x7B49;&#x7684;&#xFF0C;&#x5982;&#x679C;&#x662F;&#x5219;&#x8868;&#x793A;EXPECT_EQ&#x4F1A;&#x8FD4;&#x56DE;&#x6210;&#x529F;&#xFF0C;&#x5426;&#x5219;&#x5931;&#x8D25;&#xFF0C;&#x4E5F;&#x5373;&#x6211;&#x4EEC;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x4F1A;&#x5931;&#x8D25;&#x6216;&#x8005;&#x6210;&#x529F;&#x3002;</p>
<ul>
<li>&#x7B2C;&#x4E09;&#x6B65;&#xFF0C;&#x5B9E;&#x73B0;&#x6D4B;&#x8BD5;&#x7684;main&#x51FD;&#x6570;&#xFF0C;&#x5F53;&#x7136;&#x6211;&#x4EEC;&#x4E5F;&#x53EF;&#x4EE5;&#x4E0D;&#x7528;&#x5199;main&#x51FD;&#x6570;&#xFF0C;&#x90A3;&#x5C31;&#x9700;&#x8981;&#x8FDE;&#x63A5;gtest_main.a&#x8FD9;&#x4E2A;&#x5E93;&#x3002;&#x6BD4;&#x5982;&#x8FD9;&#x6837;&#x5B50;&#x7F16;&#x8BD1;&#xFF1A;<pre><code>g++ sample1.cc sample1_unittest.cc -lgtest -std=c++11 -lgtest_main -lpthread -o test1
</code></pre>&#x7136;&#x540E;&#x8FD0;&#x884C;&#x6D4B;&#x8BD5;&#x7A0B;&#x5E8F;test&#xFF1A;<pre><code>$ ./test
</code></pre>&#x4F1A;&#x6709;&#x4EE5;&#x4E0B;&#x8F93;&#x51FA;&#xFF1A;
<img src="5.2_1.png" alt="&#x6D4B;&#x8BD5;&#x7ED3;&#x679C;">
&#x6216;&#x8005;&#x662F;&#x81EA;&#x5DF1;&#x5199;&#x4E00;&#x4E2A;main&#x51FD;&#x6570;&#xFF0C;&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x5982;&#x4E0B;&#xFF1A;<pre><code>#include &lt;gtest/gtest.h&gt;
int main(int argc, char** argv){
  testing::InitGoogleTest(&amp;argc, argv);
  return RUN_ALL_TESTS();
}
</code></pre>&#x7136;&#x540E;&#x7F16;&#x8BD1;&#x6210;&#x6211;&#x4EEC;&#x7684;&#x6D4B;&#x8BD5;&#x7A0B;&#x5E8F;&#x5C31;OK&#x4E86;&#xFF0C;&#x4E5F;&#x8BB8;&#x5F88;&#x591A;&#x540C;&#x5B66;&#x4F1A;&#x5F88;&#x60CA;&#x8BB6;&#x4E3A;&#x4EC0;&#x4E48;RUN_ALL_TESTS&#x51FD;&#x6570;&#x540E;&#x4F1A;&#xFF0C;&#x6211;&#x4EEC;&#x7684;&#x6D4B;&#x8BD5;&#x7A0B;&#x5E8F;&#x5C31;&#x80FD;&#x81EA;&#x52A8;&#x8FD0;&#x884C;&#x4E86;&#x5462;&#xFF1F;
&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x9898;&#x5916;&#x8BDD;&#xFF0C;&#x5F53;&#x7136;&#x6211;&#x89C9;&#x5F97;&#x8FD9;&#x4E5F;&#x662F;gtest&#x7684;&#x4E00;&#x5927;&#x4EAE;&#x70B9;&#x3002;&#x6709;&#x5174;&#x8DA3;&#x4E0D;&#xFF1F;</li>
</ul>
<h2 id="4">gtest&#x7684;&#x5176;&#x5B83;&#x7279;&#x6027;</h2>

<p></p><h3 id="41">4.1.&#x4E8B;&#x4EF6;</h3>
&#x6709;&#x65F6;&#x5019;&#x5728;&#x6D4B;&#x8BD5;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x6211;&#x4EEC;&#x4F1A;&#x5728;&#x6D4B;&#x8BD5;&#x524D;&#x505A;&#x4E00;&#x4E9B;&#x521D;&#x59CB;&#x5316;&#x6D3B;&#x52A8;&#xFF0C;&#x548C;&#x6D4B;&#x8BD5;&#x540E;&#x505A;&#x4E00;&#x4E9B;&#x6E05;&#x7406;&#x5DE5;&#x4F5C;&#xFF0C;gtest&#x63D0;&#x4F9B;&#x4E86;&#x591A;&#x79CD;&#x4E8B;&#x4EF6;&#x673A;&#x5236;&#xFF0C;&#x975E;&#x5E38;&#x65B9;&#x4FBF;&#x6211;&#x4EEC;&#x5728;&#x6848;&#x4F8B;&#x4E4B;&#x524D;&#x6216;&#x4E4B;&#x540E;&#x505A;&#x4E00;&#x4E9B;&#x64CD;&#x4F5C;&#x3002;&#x603B;&#x7ED3;&#x4E00;&#x4E0B;gtest&#x7684;&#x4E8B;&#x4EF6;&#x4E00;&#x5171;&#x6709;3&#x79CD;&#xFF1A;<p></p>
<ol>
<li>&#x5168;&#x5C40;&#x7684;&#xFF0C;&#x6240;&#x6709;&#x6848;&#x4F8B;&#x6267;&#x884C;&#x524D;&#x540E;&#x3002;  </li>
<li>TestSuite&#x7EA7;&#x522B;&#x7684;&#xFF0C;&#x5728;&#x67D0;&#x4E00;&#x6279;&#x6848;&#x4F8B;&#x4E2D;&#x7B2C;&#x4E00;&#x4E2A;&#x6848;&#x4F8B;&#x524D;&#xFF0C;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x6848;&#x4F8B;&#x6267;&#x884C;&#x540E;&#x3002;  </li>
<li><p>TestCase&#x7EA7;&#x522B;&#x7684;&#xFF0C;&#x6BCF;&#x4E2A;TestCase&#x524D;&#x540E;&#x3002;  </p>
</li>
<li><p>TestCase&#x7EA7;&#x522B;&#x7684;<br>&#x4E5F;&#x5C31;&#x662F;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x7EE7;&#x627F;&#x4E8E;testing::Test&#x7C7B;&#xFF0C;&#x7136;&#x540E;&#x5B9E;&#x73B0;&#x5B83;&#x7684;SetUp&#x548C;TearDown&#x65B9;&#x6CD5;&#xFF0C;&#x6BD4;&#x5982;&#xFF1A;  </p>
</li>
</ol>
<pre><code>class QueueTestSmpl3 : public testing::Test {
 protected:

  virtual void SetUp() {
      //&#x505A;&#x4E00;&#x4E9B;&#x51C6;&#x5907;&#x5DE5;&#x4F5C;
  }

  virtual void TearDown() {
     // &#x505A;&#x4E00;&#x4E9B;&#x6E05;&#x7406;&#x5DE5;&#x4F5C;
  }
</code></pre><p> OK&#xFF0C;&#x6211;&#x4EEC;&#x6765;&#x4E00;&#x4E2A;&#x6848;&#x4F8B;&#x5427;&#xFF0C;&#x6BD4;&#x5982;&#xFF1A;</p>
<pre><code> #ifndef GTEST_SAMPLES_SAMPLE3_INL_H_
#define GTEST_SAMPLES_SAMPLE3_INL_H_

#include &lt;stddef.h&gt;

template &lt;typename E&gt;  // E is the element type
class Queue;

template &lt;typename E&gt;  // E is the element type
class QueueNode {
  friend class Queue&lt;E&gt;;

 public:
  // Gets the element in this node.
  const E&amp; element() const { return element_; }

  // Gets the next node in the queue.
  QueueNode* next() { return next_; }
  const QueueNode* next() const { return next_; }

 private:
  // Creates a node with a given element value.  The next pointer is
  // set to NULL.
  explicit QueueNode(const E&amp; an_element)
      : element_(an_element), next_(nullptr) {}

  // We disable the default assignment operator and copy c&apos;tor.
  const QueueNode&amp; operator = (const QueueNode&amp;);
  QueueNode(const QueueNode&amp;);

  E element_;
  QueueNode* next_;
};

template &lt;typename E&gt;  // E is the element type.
class Queue {
 public:
  // Creates an empty queue.
  Queue() : head_(nullptr), last_(nullptr), size_(0) {}

  // D&apos;tor.  Clears the queue.
  ~Queue() { Clear(); }

  // Clears the queue.
  void Clear() {
    if (size_ &gt; 0) {
      // 1. Deletes every node.
      QueueNode&lt;E&gt;* node = head_;
      QueueNode&lt;E&gt;* next = node-&gt;next();
      for (; ;) {
        delete node;
        node = next;
        if (node == nullptr) break;
        next = node-&gt;next();
      }

      // 2. Resets the member variables.
      head_ = last_ = nullptr;
      size_ = 0;
    }
  }

  size_t Size() const { return size_; }

  QueueNode&lt;E&gt;* Head() { return head_; }
  const QueueNode&lt;E&gt;* Head() const { return head_; }

  QueueNode&lt;E&gt;* Last() { return last_; }
  const QueueNode&lt;E&gt;* Last() const { return last_; }

  void Enqueue(const E&amp; element) {
    QueueNode&lt;E&gt;* new_node = new QueueNode&lt;E&gt;(element);

    if (size_ == 0) {
      head_ = last_ = new_node;
      size_ = 1;
    } else {
      last_-&gt;next_ = new_node;
      last_ = new_node;
      size_++;
    }
  }

  E* Dequeue() {
    if (size_ == 0) {
      return nullptr;
    }

    const QueueNode&lt;E&gt;* const old_head = head_;
    head_ = head_-&gt;next_;
    size_--;
    if (size_ == 0) {
      last_ = nullptr;
    }

    E* element = new E(old_head-&gt;element());
    delete old_head;

    return element;
  }

  template &lt;typename F&gt;
  Queue* Map(F function) const {
    Queue* new_queue = new Queue();
    for (const QueueNode&lt;E&gt;* node = head_; node != nullptr;
         node = node-&gt;next_) {
      new_queue-&gt;Enqueue(function(node-&gt;element()));
    }

    return new_queue;
  }

 private:
  QueueNode&lt;E&gt;* head_;  // The first node of the queue.
  QueueNode&lt;E&gt;* last_;  // The last node of the queue.
  size_t size_;  // The number of elements in the queue.

  // We disallow copying a queue.
  Queue(const Queue&amp;);
  const Queue&amp; operator = (const Queue&amp;);
};

#endif  // GTEST_SAMPLES_SAMPLE3_INL_H_
</code></pre><p>&#x4E0A;&#x9762;&#x662F;&#x4E00;&#x4E2A;&#x961F;&#x5217;&#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x90A3;&#x4E48;&#x6211;&#x4EEC;&#x600E;&#x4E48;&#x505A;&#x6D4B;&#x8BD5;&#x5462;&#xFF1F;&#x6BD4;&#x5982;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x6D4B;&#x8BD5;&#x5B83;&#x7684;&#x5165;&#x961F;&#x548C;&#x51FA;&#x961F;&#x65B9;&#x6CD5;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x5728;SetUp&#x65B9;&#x6CD5;&#x5165;&#x961F;&#x4E00;&#x4E9B;&#x6570;&#x636E;&#xFF0C;&#x7136;&#x540E;&#x6D4B;&#x8BD5;&#x80FD;&#x5426;&#x51FA;&#x961F;&#x4E00;&#x4E9B;&#x6B63;&#x786E;&#x7684;&#x6570;&#x636E;&#x3002;</p>
<pre><code>#include &quot;sample3-inl.h&quot;
#include &quot;gtest/gtest.h&quot;
namespace {
class QueueTestSmpl3 : public testing::Test {
 protected: 

  virtual void SetUp() {
    q0_.Enqueue(1);
    q1_.Enqueue(2);
    q2_.Enqueue(3);
  }

   virtual void TearDown() {
   }

  static int Double(int n) {
    return 2*n;
  }

  // A helper function for testing Queue::Map().
  void MapTester(const Queue&lt;int&gt; * q) {
    // Creates a new queue, where each element is twice as big as the
    // corresponding one in q.
    const Queue&lt;int&gt; * const new_q = q-&gt;Map(Double);

    // Verifies that the new queue has the same size as q.
    ASSERT_EQ(q-&gt;Size(), new_q-&gt;Size());

    // Verifies the relationship between the elements of the two queues.
    for (const QueueNode&lt;int&gt;*n1 = q-&gt;Head(), *n2 = new_q-&gt;Head();
         n1 != nullptr; n1 = n1-&gt;next(), n2 = n2-&gt;next()) {
      EXPECT_EQ(2 * n1-&gt;element(), n2-&gt;element());
    }

    delete new_q;
  }

  // Declares the variables your tests want to use.
  Queue&lt;int&gt; q0_;
  Queue&lt;int&gt; q1_;
  Queue&lt;int&gt; q2_;
};

// Tests Dequeue().
TEST_F(QueueTestSmpl3, Dequeue) {
  int * n = q0_.Dequeue();
  EXPECT_TRUE(n == nullptr);

  n = q1_.Dequeue();
  ASSERT_TRUE(n != nullptr);
  EXPECT_EQ(1, *n);
  EXPECT_EQ(0u, q1_.Size());
  delete n;

  n = q2_.Dequeue();
  ASSERT_TRUE(n != nullptr);
  EXPECT_EQ(2, *n);
  EXPECT_EQ(1u, q2_.Size());
  delete n;
}
}  // namespace
</code></pre><p>&#x6211;&#x4EEC;&#x5728;SetUp&#x91CC;&#x538B;&#x5165;&#x4E86;&#x6570;&#x636E;&#xFF0C;&#x7136;&#x540E;&#x6211;&#x4EEC;&#x6D4B;&#x8BD5;&#x662F;&#x5426;&#x80FD;&#x591F;&#x51FA;&#x961F;&#x671F;&#x671B;&#x7684;&#x6570;&#x636E;&#x3002;&#x90A3;&#x4E48;&#x8FD9;&#x4E2A;SetUp&#x6709;&#x4E00;&#x70B9;&#x597D;&#x7684;&#x662F;&#xFF0C;&#x6BCF;&#x4E00;&#x4E2A;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x8FD0;&#x884C;&#x524D;&#x90FD;&#x4F1A;&#x8C03;&#x7528;&#xFF0C;&#x6BCF;&#x4E00;&#x4E2A;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x8FD0;&#x884C;&#x540E;&#x90FD;&#x4F1A;&#x8C03;&#x7528;TearDown&#x65B9;&#x6CD5;&#x3002;</p>
<p>&#x54E6;&#xFF0C;&#x6709;&#x4E86;&#x8FD9;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x7684;&#x4E8B;&#x4EF6;&#xFF0C;&#x6211;&#x4EEC;&#x6765;&#x770B;&#x770B;&#x5982;&#x4F55;&#x5B9A;&#x4E49;&#x4E2A;&#x6D4B;&#x8BD5;&#x5957;&#x7684;&#x4E8B;&#x4EF6;&#x3002;</p>
<ul>
<li>&#x6D4B;&#x8BD5;&#x5957;&#x4E8B;&#x4EF6;  </li>
</ul>
<p>&#x8FD9;&#x4E2A;&#x76F8;&#x5BF9;&#x6765;&#x8BF4;&#x6BD4;&#x8F83;&#x7B80;&#x5355;&#xFF0C;&#x76F8;&#x5BF9;&#x4E0A;&#x9762;&#x7684;&#x5B9E;&#x73B0;&#x5C31;&#x662F;&#x66FF;&#x6362;&#x6210;&#x9759;&#x6001;&#x7684;SetUpTestCase&#xFF0C;&#x6BD4;&#x5982;&#x4E0A;&#x4EE3;&#x7801;&#x5427;&#xFF1A;  </p>
<pre><code>class FooTest : public testing::Test {
 protected:
 //&#x51C6;&#x5907;&#x8D44;&#x6E90;
  static void SetUpTestCase() {
    shared_resource_ = new ;
  }
  //&#x91CA;&#x653E;&#x8D44;&#x6E90;
  static void TearDownTestCase() {
    delete shared_resource_;
    shared_resource_ = NULL;
  }
  // &#x8D44;&#x6E90;
  static T* shared_resource_;
};
</code></pre><ul>
<li>&#x5168;&#x5C40;&#x4E8B;&#x4EF6;  </li>
</ul>
<p>&#x4E5F;&#x662F;&#x5F88;&#x7B80;&#x5355;&#x7684;&#xFF0C;&#x5C31;&#x662F;&#x7EE7;&#x627F;&#x4E8E;testing::Environment&#x800C;&#x5DF2;&#x3002;  </p>
<pre><code>class FooEnvironment: public testing::Environment
{
    public:
        virtual void SetUp()
        {
            printf(&quot;Environment SetUp!\n&quot;);
            a = 100;
        }
        virtual void TearDown()
        {
            printf(&quot;Environment TearDown!\n&quot;);
        }
        int a;     //&#x5171;&#x4EAB;&#x6570;&#x636E;
};
FooEnvironment* foo_env;  //&#x5BF9;&#x8C61;&#x6307;&#x9488;&#x58F0;&#x660E;
TEST(firstTest, first)    //&#x8BBF;&#x95EE;&#x5171;&#x4EAB;&#x6570;&#x636E;&#x5E76;&#x6539;&#x53D8;&#x5B83;&#x7684;&#x503C;
{
    printf(&quot;in the firstTest, foo_env-&gt;p is %d\n&quot;, foo_env-&gt;a);
    foo_env-&gt;a ++;
}
TEST(secondTest, second)  //&#x8BBF;&#x95EE;&#x5171;&#x4EAB;&#x6570;&#x636E;
{
    printf(&quot;in the secondTest, foo_env-&gt;p is %d\n&quot;, foo_env-&gt;a);
}
int main(int argc, char* argv[])
{
    foo_env = new FooEnvironment;
    testing::AddGlobalTestEnvironment(foo_env);     //&#x6CE8;&#x518C;
    testing::InitGoogleTest(&amp;argc, argv);
    return RUN_ALL_TESTS();
}
</code></pre><p></p><h3 id="42">4.2.&#x53C2;&#x6570;&#x5316;</h3>
<strong>&#x95EE;&#x9898;&#x7684;&#x7531;&#x6765;&#xFF0C;&#x6BD4;&#x5982;&#x6211;&#x4EEC;&#x7B2C;&#x4E00;&#x4E2A;&#x6D4B;&#x8BD5;&#x793A;&#x4F8B;&#x4E2D;&#x6709;IsPrime&#x51FD;&#x6570;&#xFF0C;&#x5224;&#x65AD;&#x662F;&#x5426;&#x4E3A;&#x7D20;&#x6570;</strong>
&#x90A3;&#x4E48;&#x6211;&#x4EEC;&#x7684;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x53EF;&#x80FD;&#x4F1A;&#x662F;&#x8FD9;&#x6837;&#xFF1A;  <p></p>
<pre><code>TEST(IsPrimeTest, HandleTrueReturn)
{
    EXPECT_TRUE(IsPrime(3));
    EXPECT_TRUE(IsPrime(5));
    EXPECT_TRUE(IsPrime(11));
    EXPECT_TRUE(IsPrime(23));
    EXPECT_TRUE(IsPrime(17));
}
</code></pre><p>&#x90A3;&#x8FD9;&#x6837;&#x4E00;&#x4E2A;&#x6D4B;&#x8BD5;&#x6709;&#x4EC0;&#x4E48;&#x95EE;&#x9898;&#x5462;&#xFF1F;&#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x8F93;&#x5165;&#x4E86;5&#x4E2A;&#x6570;&#xFF0C;&#x5982;&#x679C;&#x8981;&#x6D4B;&#x8BD5;100,10000&#x4E2A;&#x6570;&#x600E;&#x4E48;&#x529E;&#x5462;&#xFF1F;&#x96BE;&#x9053;&#x5199;&#x4E00;&#x4E07;&#x904D;EXPECT_TRUE&#x8BED;&#x53E5;&#xFF1F;&#x597D;&#x7684;&#xFF0C;&#x90A3;&#x65E2;&#x7136;&#x4E0D;&#x80FD;&#x5982;&#x6B64;&#xFF0C;&#x65B0;&#x7684;&#x65B9;&#x6848;&#x5462;&#xFF1F;&#x5C31;&#x662F;&#x4F7F;&#x7528;&#x53C2;&#x6570;&#x5316;&#x6D4B;&#x8BD5;&#x65B9;&#x6CD5;&#x3002;How to do&#xFF1F;</p>
<ul>
<li>&#x7B2C;&#x4E00;&#x6B65;&#xFF1A; &#x544A;&#x8BC9;gtest&#x4F60;&#x7684;&#x53C2;&#x6570;&#x7C7B;&#x578B;&#x662F;&#x4EC0;&#x4E48;
&#x4F60;&#x5FC5;&#x987B;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x7C7B;&#xFF0C;&#x7EE7;&#x627F;testing::TestWithParam<T>&#xFF0C;&#x5176;&#x4E2D;T&#x5C31;&#x662F;&#x4F60;&#x9700;&#x8981;&#x53C2;&#x6570;&#x5316;&#x7684;&#x53C2;&#x6570;&#x7C7B;&#x578B;&#xFF0C;&#x6BD4;&#x5982;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x6211;&#x9700;&#x8981;&#x53C2;&#x6570;&#x5316;&#x4E00;&#x4E2A;int&#x578B;&#x7684;&#x53C2;&#x6570;</T></li>
</ul>
<pre><code>class IsPrimeParamTest : public::testing::TestWithParam&lt;int&gt;
{

};
</code></pre><ul>
<li>&#x7B2C;&#x4E8C;&#x6B65;&#xFF1A;&#x544A;&#x8BC9;gtest&#x4F60;&#x62FF;&#x5230;&#x53C2;&#x6570;&#x7684;&#x503C;&#x540E;&#xFF0C;&#x5177;&#x4F53;&#x505A;&#x4E9B;&#x4EC0;&#x4E48;&#x6837;&#x7684;&#x6D4B;&#x8BD5;</li>
</ul>
<p>&#x8FD9;&#x91CC;&#xFF0C;&#x6211;&#x4EEC;&#x8981;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5B8F;&#xFF08;&#x55EF;&#xFF0C;&#x633A;&#x5174;&#x594B;&#x7684;&#xFF09;&#xFF1A;TEST_P&#xFF0C;&#x5728;TEST_P&#x5B8F;&#x91CC;&#xFF0C;&#x4F7F;&#x7528;GetParam()&#x83B7;&#x53D6;&#x5F53;&#x524D;&#x7684;&#x53C2;&#x6570;&#x7684;&#x5177;&#x4F53;&#x503C;&#xFF0C;&#x5E76;&#x4E14;&#x544A;&#x8BC9;gtest&#x6211;&#x4EEC;&#x8981;&#x505A;EXPECT_TRUE&#x6D4B;&#x8BD5;&#x3002;</p>
<pre><code>TEST_P(IsPrimeParamTest, HandleTrueReturn)
{
    int n =  GetParam();
    EXPECT_TRUE(IsPrime(n));
}
</code></pre><ul>
<li><p>&#x7B2C;&#x4E09;&#x6B65;&#xFF1A;&#x5982;&#x4F55;&#x544A;&#x8BC9;gtest&#x4F60;&#x7684;&#x6D4B;&#x8BD5;&#x53C2;&#x6570;</p>
<pre><code>INSTANTIATE_TEST_CASE_P(TrueReturn, IsPrimeParamTest, testing::Values(3, 5, 11, 23, 17));
</code></pre><p>&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x6D4B;&#x8BD5;&#x6848;&#x4F8B;&#x7684;&#x524D;&#x7F00;&#xFF0C;&#x53EF;&#x4EE5;&#x4EFB;&#x610F;&#x53D6;&#x3002; 
&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x6D4B;&#x8BD5;&#x6848;&#x4F8B;&#x7684;&#x540D;&#x79F0;&#xFF0C;&#x9700;&#x8981;&#x548C;&#x4E4B;&#x524D;&#x5B9A;&#x4E49;&#x7684;&#x53C2;&#x6570;&#x5316;&#x7684;&#x7C7B;&#x7684;&#x540D;&#x79F0;&#x76F8;&#x540C;&#xFF0C;&#x5982;&#xFF1A;IsPrimeParamTest 
&#x7B2C;&#x4E09;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x53EF;&#x4EE5;&#x7406;&#x89E3;&#x4E3A;&#x53C2;&#x6570;&#x751F;&#x6210;&#x5668;&#xFF0C;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4F7F;&#x7528;test::Values&#x8868;&#x793A;&#x4F7F;&#x7528;&#x62EC;&#x53F7;&#x5185;&#x7684;&#x53C2;&#x6570;&#x3002;Google&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x7CFB;&#x5217;&#x7684;&#x53C2;&#x6570;&#x751F;&#x6210;&#x7684;&#x51FD;&#x6570;&#xFF1A;</p>
<ul>
<li>Range(begin, end[, step])    &#xFF1A;&#x8303;&#x56F4;&#x5728;begin~end&#x4E4B;&#x95F4;&#xFF0C;&#x6B65;&#x957F;&#x4E3A;step&#xFF0C;&#x4E0D;&#x5305;&#x62EC;end</li>
<li>Values(v1, v2, ..., vN)      &#xFF1A; v1,v2&#x5230;vN&#x7684;&#x503C;</li>
<li>ValuesIn(container) and ValuesIn(begin, end)    &#xFF1A;  &#x4ECE;&#x4E00;&#x4E2A;C&#x7C7B;&#x578B;&#x7684;&#x6570;&#x7EC4;&#x6216;&#x662F;STL&#x5BB9;&#x5668;&#xFF0C;&#x6216;&#x662F;&#x8FED;&#x4EE3;&#x5668;&#x4E2D;&#x53D6;&#x503C;</li>
<li>Bool()    &#xFF1A; &#x53D6;false &#x548C; true &#x4E24;&#x4E2A;&#x503C;</li>
<li>Combine(g1, g2, ..., gN)    &#xFF1A;&#x8FD9;&#x4E2A;&#x6BD4;&#x8F83;&#x5F3A;&#x608D;&#xFF0C;&#x5B83;&#x5C06;g1,g2,...gN&#x8FDB;&#x884C;&#x6392;&#x5217;&#x7EC4;&#x5408;&#xFF0C;g1,g2,...gN&#x672C;&#x8EAB;&#x662F;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x751F;&#x6210;&#x5668;&#xFF0C;&#x6BCF;&#x6B21;&#x5206;&#x522B;&#x4ECE;g1,g2,..gN&#x4E2D;&#x5404;&#x53D6;&#x51FA;&#x4E00;&#x4E2A;&#x503C;&#xFF0C;&#x7EC4;&#x5408;&#x6210;&#x4E00;&#x4E2A;&#x5143;&#x7EC4;(Tuple)&#x4F5C;&#x4E3A;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x3002;&#x8BF4;&#x660E;&#xFF1A;&#x8FD9;&#x4E2A;&#x529F;&#x80FD;&#x53EA;&#x5728;&#x63D0;&#x4F9B;&#x4E86;<tr1 tuple="">&#x5934;&#x7684;&#x7CFB;&#x7EDF;&#x4E2D;&#x6709;&#x6548;&#x3002;gtest&#x4F1A;&#x81EA;&#x52A8;&#x53BB;&#x5224;&#x65AD;&#x662F;&#x5426;&#x652F;&#x6301;tr/tuple&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x7684;&#x7CFB;&#x7EDF;&#x786E;&#x5B9E;&#x652F;&#x6301;&#xFF0C;&#x800C;gtest&#x5224;&#x65AD;&#x9519;&#x8BEF;&#x7684;&#x8BDD;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x91CD;&#x65B0;&#x5B9A;&#x4E49;&#x5B8F;GTEST_HAS_TR1_TUPLE=1&#x3002;</tr1></li>
</ul>
</li>
</ul>
<p></p><h3 id="43">4.3.&#x6B7B;&#x4EA1;&#x6D4B;&#x8BD5;</h3>
&#x6B7B;&#x4EA1;&#x6D4B;&#x8BD5;&#x201D;&#x540D;&#x5B57;&#x6BD4;&#x8F83;&#x6050;&#x6016;&#xFF0C;&#x8FD9;&#x91CC;&#x7684;&#x201C;&#x6B7B;&#x4EA1;&#x201D;&#x6307;&#x7684;&#x7684;&#x662F;&#x7A0B;&#x5E8F;&#x7684;&#x5D29;&#x6E83;&#x3002;&#x901A;&#x5E38;&#x5728;&#x6D4B;&#x8BD5;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x8003;&#x8651;&#x5404;&#x79CD;&#x5404;&#x6837;&#x7684;&#x8F93;&#x5165;&#xFF0C;&#x6709;&#x7684;&#x8F93;&#x5165;&#x53EF;&#x80FD;&#x76F4;&#x63A5;&#x5BFC;&#x81F4;&#x7A0B;&#x5E8F;&#x5D29;&#x6E83;&#xFF0C;&#x8FD9;&#x65F6;&#x6211;&#x4EEC;&#x5C31;&#x9700;&#x8981;&#x68C0;&#x67E5;&#x7A0B;&#x5E8F;&#x662F;&#x5426;&#x6309;&#x7167;&#x9884;&#x671F;&#x7684;&#x65B9;&#x5F0F;&#x6302;&#x6389;&#xFF0C;&#x8FD9;&#x4E5F;&#x5C31;&#x662F;&#x6240;&#x8C13;&#x7684;&#x201C;&#x6B7B;&#x4EA1;&#x6D4B;&#x8BD5;&#x201D;&#x3002;gtest&#x7684;&#x6B7B;&#x4EA1;&#x6D4B;&#x8BD5;&#x80FD;&#x505A;&#x5230;&#x5728;&#x4E00;&#x4E2A;&#x5B89;&#x5168;&#x7684;&#x73AF;&#x5883;&#x4E0B;&#x6267;&#x884C;&#x5D29;&#x6E83;&#x7684;&#x6D4B;&#x8BD5;&#x6848;&#x4F8B;&#xFF0C;&#x540C;&#x65F6;&#x53C8;&#x5BF9;&#x5D29;&#x6E83;&#x7ED3;&#x679C;&#x8FDB;&#x884C;&#x9A8C;&#x8BC1;&#x3002;&#x8FD9;&#x91CC;&#x5C31;&#x4E0D;&#x8BE6;&#x8FF0;&#x4E86;&#xFF0C;&#x6709;&#x7528;&#x5230;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x6211;&#x4EEC;&#x518D;&#x5355;&#x72EC;&#x8BA8;&#x8BBA;&#xFF0C;&#x5F53;&#x7136;&#x5927;&#x5BB6;&#x4EFB;&#x4F55;&#x65F6;&#x95F4;&#x90FD;&#x53EF;&#x4EE5;&#x627E;Lee&#x54E5;&#x3002;<p></p>
<p></p><h3 id="44">4.4.&#x6DF1;&#x5165;&#x89E3;&#x6790;gtest&#x6846;&#x67B6;</h3>
&#x6211;&#x4EEC;&#x6765;&#x56DE;&#x5FC6;&#x4E0B;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x7684;&#x5199;&#x6CD5;&#xFF1A;  <p></p>
<pre><code>#include &lt;gtest/gtest.h&gt;
int main(int argc, char** argv){
    testing::InitGoogleTest(&amp;argc, argv);
    return RUN_ALL_TESTS();
}

// Tests factorial of negative numbers.  
TEST(FactorialTest, Factorial_parameter_is_negative_num) {  
  EXPECT_EQ(1, Factorial(-5));  
  EXPECT_EQ(1, Factorial(-1));  
  EXPECT_GT(Factorial(-10), 0);  
}
</code></pre><p>&#x5728;main&#x51FD;&#x6570;&#x91CC;&#xFF0C;&#x6211;&#x4EEC;&#x6839;&#x672C;&#x5C31;&#x6CA1;&#x6709;&#x5199;&#x8C03;&#x7528;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x90A3;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x90FD;&#x662F;&#x600E;&#x4E48;&#x8DD1;&#x8D77;&#x6765;&#x7684;&#x6765;&#xFF1F;&#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x5C31;&#x8981;&#x5206;&#x6790;&#x5206;&#x6790;TEST&#x8FD9;&#x4E2A;&#x4E1C;&#x897F;&#xFF0C;&#x5176;&#x5B9E;&#x6211;&#x4EEC;&#x53BB;&#x7FFB;&#x770B;gtest&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x4F1A;&#x53D1;&#x73B0;TEST&#x662F;&#x4E00;&#x4E2A;&#x5B8F;&#x5B9A;&#x4E49;&#xFF0C;&#x5176;&#x5B9A;&#x4E49;&#x5982;&#x4E0B;&#xFF1A;  </p>
<pre><code>#if !GTEST_DONT_DEFINE_TEST
# define TEST(test_case_name, test_name) GTEST_TEST(test_case_name, test_name)
#endif

#define GTEST_TEST(test_case_name, test_name)\
  GTEST_TEST_(test_case_name, test_name, \
              ::testing::Test, ::testing::internal::GetTestTypeId())

#define GTEST_TEST_(test_case_name, test_name, parent_class, parent_id)\
class GTEST_TEST_CLASS_NAME_(test_case_name, test_name) : public parent_class {\
 public:\
  GTEST_TEST_CLASS_NAME_(test_case_name, test_name)() {}\
 private:\
  virtual void TestBody();\
  static ::testing::TestInfo* const test_info_ GTEST_ATTRIBUTE_UNUSED_;\
  GTEST_DISALLOW_COPY_AND_ASSIGN_(\
      GTEST_TEST_CLASS_NAME_(test_case_name, test_name));\
};\
\
::testing::TestInfo* const GTEST_TEST_CLASS_NAME_(test_case_name, test_name)\
  ::test_info_ =\
    ::testing::internal::MakeAndRegisterTestInfo(\
        #test_case_name, #test_name, NULL, NULL, \
        ::testing::internal::CodeLocation(__FILE__, __LINE__), \
        (parent_id), \
        parent_class::SetUpTestCase, \
        parent_class::TearDownTestCase, \
        new ::testing::internal::TestFactoryImpl&lt;\
            GTEST_TEST_CLASS_NAME_(test_case_name, test_name)&gt;);\
void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::TestBody()
</code></pre><p>&#x770B;&#x4E86;&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x662F;&#x4E0D;&#x662F;&#x89C9;&#x5F97;&#x5F88;&#x6655;&#x4E86;&#xFF0C;&#x4E0D;&#x8981;&#x6655;&#x554A;&#xFF0C;&#x8FD9;&#x662F;&#x5B8F;&#x975E;&#x5E38;&#x7F8E;&#x5999;&#x7684;&#x7528;&#x6CD5;&#xFF0C;&#x552F;&#x4E00;&#x8981;&#x63D0;&#x7684;&#x5C31;&#x662F;#test_case_name&#x548C;#test_name&#xFF0C;&#x8FD9;&#x4E2A;&#x201C;#&#x201D;&#x5C31;&#x662F;C&#x8BED;&#x8A00;&#x91CC;&#x4E00;&#x4E2A;&#x7279;&#x5B9A;&#x7684;&#x5B8F;&#xFF0C;&#x5C31;&#x662F;&#x8F6C;&#x6362;&#x6210;&#x5B57;&#x7B26;&#x4E32;&#x3002;&#x90A3;OK&#xFF0C;&#x8FD9;&#x91CC;&#x7684;MakeAndRegisterTestInfo&#x662F;&#x4EC0;&#x4E48;&#x4E1C;&#x897F;&#xFF1A;</p>
<pre><code>TestInfo* MakeAndRegisterTestInfo(
    const char* test_case_name,
    const char* name,
    const char* type_param,
    const char* value_param,
    CodeLocation code_location,
    TypeId fixture_class_id,
    SetUpTestCaseFunc set_up_tc,
    TearDownTestCaseFunc tear_down_tc,
    TestFactoryBase* factory) {
  TestInfo* const test_info =
      new TestInfo(test_case_name, name, type_param, value_param,
                   code_location, fixture_class_id, factory);
  GetUnitTestImpl()-&gt;AddTestInfo(set_up_tc, tear_down_tc, test_info);
  return test_info;

 inline UnitTestImpl* GetUnitTestImpl() {
      return UnitTest::GetInstance()-&gt;impl();
  }
}
</code></pre><p>&#x4E5F;&#x5C31;&#x662F;&#x628A;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x6DFB;&#x52A0;&#x5230;UnitTest::GetInstance()-&gt;impl()&#x91CC;&#x4E86;&#xFF0C;AddTestInfo&#x7684;&#x5B9E;&#x73B0;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>void TestCase::AddTestInfo(TestInfo * test_info) {
  test_info_list_.push_back(test_info);
  test_indices_.push_back(static_cast&lt;int&gt;(test_indices_.size()));
}
</code></pre><p>&#x90A3;&#x6211;&#x4EEC;&#x518D;&#x6765;&#x770B;&#x770B;&#x6267;&#x884C;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x65F6;&#x8C03;&#x7528;&#x7684;RUN_ALL_TESTS&#x7684;&#x5B9E;&#x73B0;&#x5427;&#x3002;</p>
<pre><code>inline int RUN_ALL_TESTS() {
  return ::testing::UnitTest::GetInstance()-&gt;Run();
}
</code></pre><p>&#x6211;&#x4EEC;&#x53C8;&#x4E00;&#x6B21;&#x770B;&#x5230;&#x4E86;UnitTest::GetInstance()&#xFF0C;&#x7136;&#x540E;&#x8C03;&#x7528;run&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre><code>// Runs every test in this TestCase.
void TestCase::Run() {
  if (!should_run_) return;

  internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();
  impl-&gt;set_current_test_case(this);

  TestEventListener* repeater = UnitTest::GetInstance()-&gt;listeners().repeater();

  repeater-&gt;OnTestCaseStart(*this);
  impl-&gt;os_stack_trace_getter()-&gt;UponLeavingGTest();
  internal::HandleExceptionsInMethodIfSupported(
      this, &amp;TestCase::RunSetUpTestCase, &quot;SetUpTestCase()&quot;);

  const internal::TimeInMillis start = internal::GetTimeInMillis();
  for (int i = 0; i &lt; total_test_count(); i++) {
    GetMutableTestInfo(i)-&gt;Run();
  }
  elapsed_time_ = internal::GetTimeInMillis() - start;

  impl-&gt;os_stack_trace_getter()-&gt;UponLeavingGTest();
  internal::HandleExceptionsInMethodIfSupported(
      this, &amp;TestCase::RunTearDownTestCase, &quot;TearDownTestCase()&quot;);

  repeater-&gt;OnTestCaseEnd(*this);
  impl-&gt;set_current_test_case(NULL);
}
</code></pre>
                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../chapter5/5.1_lua语言介绍.html" class="navigation navigation-prev " aria-label="Previous page: lua语言介绍"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../chapter5/5.3_搭建持续集成环境.html" class="navigation navigation-next " aria-label="Next page: 搭建持续集成环境"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-livereload/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"theme-default":{"showLevel":false},"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"livereload":{}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
